package com.tech.connect.huanzhewan;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.request.RequestOptions;
import com.ksy.common.image.ImageLoader;
import com.ksy.common.utils.AndroidUtil;
import com.ksy.common.utils.BaseAdapter;
import com.ksy.common.utils.BaseHolder;
import com.ksy.common.view.GridOffsetsItemDecoration;
import com.tech.connect.R;
import com.tech.connect.activity.PublishBaoWuActivity;
import com.tech.connect.activity.SearchActivity;
import com.tech.connect.fragment.BaseFragment;

import java.util.ArrayList;
import java.util.List;

public class HuanZheWanHomeFragment extends BaseFragment {


    private RecyclerView recycler;
    private List<String> allList = new ArrayList<>();
    private BaseAdapter<String, BaseHolder> adapter;
    private HuanZheWanMainFragment.TitleListener titleListener;
    private int height;

    @Override
    protected View onCreateView(LayoutInflater inflater) {
        View inflate = inflater.inflate(R.layout.fragment_huanzhewan_main, null);
        return (inflate);
    }

    @Override
    protected boolean isShowHeadBar() {
        return true;
    }

    @Override
    protected boolean isFullStateBar() {
        return true;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getHeadBar().setLeftIcon(R.drawable.icon_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.onBackPressed();
            }
        }).setTitle("换着玩", 0, R.drawable.icon_arrow_down_white).setTitleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (titleListener != null) {
                    titleListener.onFragmentTitleClick();
                }
            }
        }).setRightIcon(R.drawable.icon_search, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(SearchActivity.class);
            }
        });
        initView(view);
        initAdapter();
    }


    private void initView(View view) {
        int screenWidth = AndroidUtil.getScreenWidth(activity);
        height = (screenWidth - AndroidUtil.dip2px(30)) / 2;
        view.findViewById(R.id.llWoYouShenMe).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(PublishBaoWuActivity.class);
            }
        });
        view.findViewById(R.id.llWoHuanShenMe).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(SearchActivity.class);
            }
        });
        view.findViewById(R.id.llShiYongShuoMing).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        view.findViewById(R.id.llHuanZheWan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(HuanZheWanActivity.class);
            }
        });
        view.findViewById(R.id.llYongJiuHuan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(HuanZheWanActivity.class);

            }
        });
        recycler = view.findViewById(R.id.recycler);
        recycler.setLayoutManager(new GridLayoutManager(activity, 2) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        GridOffsetsItemDecoration itemDecoration = new GridOffsetsItemDecoration(GridOffsetsItemDecoration.GRID_OFFSETS_VERTICAL);
        itemDecoration.setHorizontalItemOffsets(AndroidUtil.dip2px(10));
        itemDecoration.setVerticalItemOffsets(AndroidUtil.dip2px(10));
        recycler.addItemDecoration(itemDecoration);
    }

    private void initAdapter() {
        if (adapter == null) {
            adapter = new BaseAdapter<String, BaseHolder>(R.layout.item_layout_huanzhewan, allList, 6) {
                @Override
                protected void convert(BaseHolder helper, int position) {
                    ImageView ivImage = helper.getView(R.id.ivImage);
                    ImageLoader.getInstance().load(activity, R.drawable.common_icon_image_placeholder, ivImage, new RequestOptions().override(height));

                }
            };
            adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Intent intent = new Intent(activity, HuanZheWanWuPinViewActivity.class);
                    intent.putExtra("wuPinTitle", "苹果6s");
                    jump2Activity(intent);
                }
            });
            recycler.setAdapter(adapter);
        }
        adapter.notifyDataSetChanged();
    }

    public void setTitleListener(HuanZheWanMainFragment.TitleListener titleListener) {
        this.titleListener = titleListener;
    }
}
